package com.hgk.wanwan.chatactivity.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.common.nostra13.universalimageloader.core.DisplayImageOptions;
import com.hgk.common.common.ui.view.CircularImageView;
import com.hgk.common.common.util.LoadImageDisplayOption;
import com.hgk.wanwan.chatactivity.molde.ChatBean;
import com.hgk.wanwan.chatactivity.molde.MsgIfo;

import java.util.List;

import width.hgk.wanwan.R;

/**
 * Created by lenovo on 2016/10/13.
 */
public class ChatAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater inflater;
    private DisplayImageOptions displayImageOptions;
    private List<MsgIfo> mList;

    public ChatAdapter(Context mContext,List<MsgIfo> list) {
        this.mContext = mContext;
        inflater = LayoutInflater.from(mContext);
        displayImageOptions = LoadImageDisplayOption.getDefaultIcon(R.drawable.icon);
        this.mList = list;
    }
    public void addDataToAdapter(MsgIfo e){
        mList.add(e);
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        ChatBean.Data data = (ChatBean.Data) getItem(position);
        if ("1".equals(data.getType())){ //本人
            return 0;
        }else if("2".equals(data.getType())){//他人
            return 1;
        }else if("3".equals(data.getType())){//系统在群里提供信息
            return 2;
        }
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder vh = null;
        int type = getItemViewType(position);
        if (convertView==null){
            vh = new ViewHolder();
            convertView = inflater.inflate(R.layout.chat_listview_item, null);
            switch (type){
                case 0:
                    vh.mContent = (TextView) convertView.findViewById(R.id.text_right);
                    vh.mHeaderPic= (CircularImageView) convertView.findViewById(R.id.civ_header_pic_right);
                    vh.mNickName = (TextView) convertView.findViewById(R.id.tv_nick_name_right);
                    break;
                case 1:
                    vh.mContent = (TextView) convertView.findViewById(R.id.text_left);
                    vh.mHeaderPic= (CircularImageView) convertView.findViewById(R.id.civ_header_pic_right);
                    vh.mNickName = (TextView) convertView.findViewById(R.id.tv_nick_name_left);
                    break;
                case 2:
                    vh.mSysPrompt = (TextView) convertView.findViewById(R.id.tv_system_prompt);
                    break;
            }
            convertView.setTag(vh);
        }else{
            vh = (ViewHolder) convertView.getTag();
        }

        ChatBean.Data data = (ChatBean.Data) getItem(position);


        return convertView;
    }
    public class ViewHolder{
        private CircularImageView mHeaderPic;
        private TextView mNickName,mContent,mSysPrompt;
    }
}
